package com.routing.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;
import java.util.Random;

public class EmitLogDirectSimple {
    public static final String MYDIRECT = "dddd";
    public static void main(String[] args) {
        ConnectionFactory factory = new ConnectionFactory();
        try(Connection connection = factory.newConnection();
            Channel channel = connection.createChannel()
        ){
            String routingKey = getRoutingKey();
            System.out.println("随机得到的key：" + routingKey);
            channel.basicPublish(MYDIRECT,routingKey,null,"abc".getBytes(StandardCharsets.UTF_8));
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    private static String getRoutingKey(){
        String[] keys = {"info","warning","error"};

        Random random = new Random();
        int i = random.nextInt(keys.length);
        return keys[i];
    }
}
